//
//  LHAFNTools.m
//  Project_B
//
//  Created by dllo on 15/12/23.
//  Copyright © 2015年 ZY. All rights reserved.
//

#import "LHAFNTools.h"

@implementation LHAFNTools
+ (void)appToolsWithSourcePath:(NSString *)path block:(SourceBlock)block {
    
    //    编码
    NSString *pathEncod = [path stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];

    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    [manager GET:pathEncod parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        
         block(responseObject); //返回数据
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
         NSLog(@"数据请求失败");

    }];
   
}

//post请求数据
+ (void)appPostToolsWithSourcePath:(NSString *)path body:(NSString *)body block:(SourcePostBlock)block {
    //    编码
    NSString *pathEncod = [path stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    
    //body体
    //    @"cacheKey=Recipe.getFindRecipe&sign=&uid=&uuid=02288be08f4b871a69565746255b0de9"
    
    //***** body 没写好
    NSDictionary *para = @{@"cacheKey":@"Recipe.getFindRecipe", @"uuid":@"02288be08f4b871a69565746255b0de9"};
    
    //[NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];  代表支持所有的接口类型
    
    
    AFHTTPSessionManager *netManager = [AFHTTPSessionManager manager];
    
//    AFHTTPRequestOperationManager *netManager = [AFHTTPRequestOperationManager manager];
    netManager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
    
    [netManager POST:path parameters:para constructingBodyWithBlock:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *dic = responseObject;
        NSLog(@"post请求成功====%@", dic);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"错误信息%@", error);

    }];
   
    
}


+ (void)appToolsWithImagePath:(NSString *)path block:(ImageBlock)block {
    //    中文编码
    NSString *pathEncode = [path stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    //    生成系统能识别的url
    NSURL *url = [NSURL URLWithString:pathEncode];
    //    创建网络连接对象
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    //    创建请求对象
    NSURLSession *session = [NSURLSession sharedSession];
    //    创建普通数据请求任务
    NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        
        block(data);  //请求下来的数据
    }];
    
    [task resume]; //开始请求任务
}


@end
